package cn.lxg.controller.user;

import cn.lxg.pojo.*;
import cn.lxg.service.*;
import cn.lxg.vo.CommentVO;
import cn.lxg.vo.TTaoLunContentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller(value = "userUserBookrackController")
@RequestMapping(value = "/page/user")
public class UserBookrackController {
    @Autowired
    private final IUserBookrackService userBookrackService = null;

    @Autowired
    private final INovelService novelService = null;

    @GetMapping("/user_bookrack_list")
    public String listPage(
            HttpSession session,
            Model model,
            @RequestParam(value = "limit", required = false, defaultValue = "15") Integer limit,
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            TUserBookrack userBookrack) {
        // 准备数据
        TUser sessionUser = (TUser) session.getAttribute("user");
        userBookrack.setUserId(sessionUser.getId());
        List<TUserBookrack> userBookrackList = this.userBookrackService.getList(limit, page, userBookrack);
        int dataCount = this.userBookrackService.countDataLine(userBookrack);
        int pageIndex = page;
        int pageCount = (int) Math.ceil((float) dataCount / limit);

        // 传递数据
        model.addAttribute("userBookrack", userBookrack);
        model.addAttribute("userBookrackList", userBookrackList);
        model.addAttribute("dataCount", dataCount);
        model.addAttribute("defaultLimit", limit);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("pageCount", pageCount);

        return "user/userBookrack/list";
    }

    /**
     * 私人文章
     */
    @GetMapping("/user_siren_list")
    public String listPageSiren(
            Model model,
            @RequestParam(value = "limit", required = false, defaultValue = "15") Integer limit,
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            TNovel novel) {
        // 预处理参数
        if (novel.getName() != null)
            novel.setName(novel.getName().trim());
        if (novel.getSynopsis() != null)
            novel.setSynopsis(novel.getSynopsis().trim());
        TNovelGroup novelGroup = new TNovelGroup();

        // 准备数据
        List<TNovel> novelList = this.novelService.getList2(limit, page, novel);
        List<TNovelGroup> novelGroupList = this.novelGroupService.getList(null, null, novelGroup);
        int dataCount = this.novelService.countDataLine(novel);
        int pageIndex = page;
        int pageCount = (int) Math.ceil((float) dataCount / limit);

        // 传递数据
        model.addAttribute("novel", novel);
        model.addAttribute("novelList", novelList);
        model.addAttribute("novelGroupList", novelGroupList);
        model.addAttribute("dataCount", dataCount);
        model.addAttribute("defaultLimit", limit);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("pageCount", pageCount);

        return "user/siren/list";
    }

    @Autowired
    INovelGroupService novelGroupService;

    /**
     * 付费文章
     */
    @GetMapping("/user_fufei_list")
    public String listPage(
            Model model,
            @RequestParam(value = "limit", required = false, defaultValue = "15") Integer limit,
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            TNovel novel) {
        // 预处理参数
        if (novel.getName() != null)
            novel.setName(novel.getName().trim());
        if (novel.getSynopsis() != null)
            novel.setSynopsis(novel.getSynopsis().trim());
        TNovelGroup novelGroup = new TNovelGroup();

        // 准备数据
        List<TNovel> novelList = this.novelService.getList1(limit, page, novel);
        List<TNovelGroup> novelGroupList = this.novelGroupService.getList(null, null, novelGroup);
        int dataCount = this.novelService.countDataLine(novel);
        int pageIndex = page;
        int pageCount = (int) Math.ceil((float) dataCount / limit);

        // 传递数据
        model.addAttribute("novel", novel);
        model.addAttribute("novelList", novelList);
        model.addAttribute("novelGroupList", novelGroupList);
        model.addAttribute("dataCount", dataCount);
        model.addAttribute("defaultLimit", limit);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("pageCount", pageCount);

        return "user/fufei/list";
    }

    @Autowired
    TaoLunServiceImpl taoLunService;

    /**
     * 主题讨论区
     */
    @GetMapping("/user_taolun_list")
    public String listPageTaolun(
            Model model,
            @RequestParam(value = "limit", required = false, defaultValue = "15") Integer limit,
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "taolunId", required = false, defaultValue = "-1") Integer taolunId,
            @RequestParam(value = "title", required = false, defaultValue = "all") String title,
            TTaoLunContent content) {
        // 预处理参数
//        if (novel.getName() != null)
//            novel.setName(novel.getName().trim());
//        if (novel.getSynopsis() != null)
//            novel.setSynopsis(novel.getSynopsis().trim());
//        TNovelGroup novelGroup = new TNovelGroup();
        System.out.println(taolunId + title);

        if (taolunId != -1) {
            content.setId(taolunId);
        }
        if (!title.equals("all")) {
            content.setTitle(title);
        }
        // 准备数据
        List<TTaoLunTitle> taolunTitleList = taoLunService.getTitleList();
        List<TTaoLunContent> taoLunContentList = taoLunService.getContentList(content);
        int dataCount = taoLunService.countDataLine(content);
        int pageIndex = page;
        int pageCount = (int) Math.ceil((float) dataCount / limit);

        // 传递数据
        model.addAttribute("taolunTitleList", taolunTitleList);
        model.addAttribute("taoLunContentList", taoLunContentList);
        model.addAttribute("dataCount", dataCount);
        model.addAttribute("defaultLimit", limit);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("pageCount", pageCount);

        return "user/taolun/list";
    }

    @GetMapping(value = "/add_taolun")
    public String addPage(Model model, HttpSession session) {
        TUser user = (TUser) session.getAttribute("user");
        model.addAttribute("user", user);
        return "user/taolun/add";
    }

    /**
     * 创建主题
     */
    @PostMapping(value = "/add_TaoLun")
    public String addTaoLun(Model model, TTaoLunTitle lunTitle) {
        // 预处理数据
        System.out.println(lunTitle);

        // 准备数据
        String msg = taoLunService.addTaoLun(lunTitle);

        // 传输数据给前端
        model.addAttribute("msg", msg);

        return "common/message";
    }

    /**
     * 创建主题
     */
    @PostMapping(value = "/taoLunContent")
    public String addTaoLun1(Model model, TTaoLunContentVO lunContent1) {
        // 预处理数据
        TTaoLunContent lunContent = new TTaoLunContent();
        lunContent.setContent(lunContent1.getContent());
        lunContent.setName(lunContent1.getName());
        lunContent.setTitle(lunContent1.getTitle());
        // 准备数据
        String msg = taoLunService.addTaoLunContent(lunContent);

        // 传输数据给前端
        model.addAttribute("msg", msg);

        return "common/message";
    }

    /**
     * 创建用户主题评论
     */
    @GetMapping(value = "/add_taolun1/{id}")
    public String addTaoLun1(Model model, HttpSession session, @PathVariable(value = "id") Integer id) {

        TTaoLunTitle lunTitle = taoLunService.getOneTitleById(id);
        String title = lunTitle.getTitle();
        // 准备数据

        model.addAttribute("user", session.getAttribute("user"));
        // 传输数据给前端
        model.addAttribute("title", title);

        return "user/taolun/add1";
    }


    /**
     * 添加评论
     *
     * @return 数据
     */
    @GetMapping(value = "/add_comment/{id}")
    public String addPage1(Model model, @PathVariable(value = "id") Integer id) {
        // 准备数据
        TNovel novel = this.novelService.get(id);
        // 传递数据
        model.addAttribute("novel", novel);

        return "admin/comment/add";
    }

    @GetMapping(value = "/add_user_bookrack/{novel_id}")
    public String addPage(Model model, @PathVariable(value = "novel_id") Integer novelId) {
        // 准备数据
        TNovel novel = this.novelService.get(novelId);

        // 传递数据
        model.addAttribute("novel", novel);

        return "user/userBookrack/add";
    }

    @PostMapping(value = "/user_bookrack/{novel_id}")
    public String add(HttpSession session, Model model, @PathVariable(value = "novel_id") Integer novelId) {
        // 预处理数据
        TUser sessionUser = (TUser) session.getAttribute("user");
        TUserBookrack userBookrack = new TUserBookrack();
        userBookrack.setUserId(sessionUser.getId());
        userBookrack.setNovelId(novelId);

        // 准备数据
        String msg = this.userBookrackService.add(userBookrack);

        // 传输数据给前端
        model.addAttribute("msg", msg);

        return "common/message";
    }

    @GetMapping(value = "/delete_user_bookrack/{id}")
    public String deletePage(Model model, @PathVariable(value = "id") Integer id) {
        // 准备数据
        TNovel novel = this.novelService.get(id);

        // 传递数据
        model.addAttribute("novel", novel);

        return "user/siren/delete";
    }

    @GetMapping(value = "/delete_user_bookracks/{id}")
    public String deletePage1(Model model, @PathVariable(value = "id") Integer id) {
        // 准备数据
        TUserBookrack userBookrack = this.userBookrackService.get(id);

        // 传递数据
        model.addAttribute("userBookrack", userBookrack);

        return "user/userBookrack/delete";
    }

    /**
     * 删除数据
     */
    @DeleteMapping(value = "/user_delete/{id}")
    public String delete1(Model model, @PathVariable(value = "id") Integer id) {
        // 准备数据
        String msg = this.novelService.delete(id);

        // 传输数据给前端
        model.addAttribute("msg", msg);

        return "common/message";
    }

    /**
     * 取消收藏
     */
    @DeleteMapping(value = "/user_bookrack/{id}")
    public String delete(Model model, @PathVariable(value = "id") Integer id) {
        // 准备数据
        String msg = this.userBookrackService.delete(id);

        // 传输数据给前端
        model.addAttribute("msg", msg);

        return "common/message";
    }

    @Autowired
    ICommentService commentService;

    /**
     * 添加评论
     */
    @PostMapping(value = "/add_comment")
    public String addComment(Model model, CommentVO comment) {
        // 参数预处理
        comment.setContent(comment.getContent().trim());

        // 准备数据
        String msg = this.commentService.add(comment);

        // 传输数据给前端
        model.addAttribute("msg", msg);

        return "common/message";
    }
}
